#pragma once
#include <stdexcept>

inline unsigned last_fib_num(unsigned n)
{
    if (n < 1 || n > 10000000)
        throw std::runtime_error("Invalid input value. Value should be in range [1, 10^7].");

    if (n == 1)
        return 1;

    unsigned fn = 0;
    unsigned fn1 = 1;

    while (--n > 0)
    {
        const unsigned tmp = fn1;

        fn1 = (fn + fn1) % 10;
        fn = tmp;
    }

    return fn1;
}

